package com.dialog;


import java.util.List;

import model.LTModel;

import org.xutils.DbManager;

import com.friehome.app.R;
import com.utils.Comm;
import com.utils.TTUtils;
import com.widget.SAlertDialog;

import adapter.LTAdapter;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

/**
 * 选择照片来源dialog
 * @author tangtang
 *
 */
public class LTDialog{
	Activity context;
	DialogClickListener photoClickListener;
	
	List<LTModel> data;
	public LTDialog(Activity context,List<LTModel> data,DialogClickListener photoClickListener) {
		this.context = context;
		this.photoClickListener = photoClickListener;
		this.data = data;
		
		try {
			DbManager db = TTUtils.getDbManager();
			this.data = db.selector(LTModel.class).findAll();
			LTModel m = new LTModel();
			m.name = "全部";
			m.id = "";
			
			this.data.add(0,m);
					
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public void showDialog(){
		
			final SAlertDialog dialog = new SAlertDialog(context, R.style.AlertDialogCustom);
			View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_store_view, null);
			
	
			ListView listView = (ListView) dialogView.findViewById(R.id.listView);
			listView.setLayoutParams(new LinearLayout.LayoutParams(TTUtils.dip2px(context, 500), TTUtils.dip2px(context, 300)));

			listView.setAdapter(new LTAdapter(context, data));
			listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					// TODO Auto-generated method stub
					photoClickListener.onBack(data.get(position).id,data.get(position).name);
					if (dialog != null) {
						dialog.cancel();
					}
				}
			});
			dialog.setView(dialogView);
			dialog.setLp(new LayoutParams(Comm.pWidth ,  LayoutParams.MATCH_PARENT));
			
			dialog.show();
		
	};
	
	
	public interface DialogClickListener{
		void onBack(String id,String name);
	}

}
